package com.qcby.community.service.impl;

import com.qcby.community.entity.Menu;
import com.qcby.community.mapper.MenuMapper;
import com.qcby.community.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.community.vo.ChildMenuRouterVO;
import com.qcby.community.vo.MenuRoleVO;
import com.qcby.community.vo.MenuRouterVO;
import com.qcby.community.vo.MetaVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-25
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 按照要求返回指定格式的用户可以访问的所有菜单
     * 数据库查询所有菜单再进行封装
     * @param userId
     * @return
     */
    @Override
    public List<MenuRouterVO> getMenuRouterByUserId(Integer userId) {
        //1.根据用户的id查询该用户所对应的角色以及该角色所对应的菜单
        List<Menu> menuList = menuMapper.getMenusByUserId(userId);
        //2.创建一个集合List<MenuRouterVO> 最终的集合
        List<MenuRouterVO> list = new ArrayList<>();
        //3.遍历该用户所能查看的所有菜单，找到一级菜单，封装进MenuRouterVO
        for (Menu menu : menuList) {
            //前提parentId=0
            if(menu.getParentId() == 0){
                //创建一个新的父级菜单对象
                MenuRouterVO menuRouterVO = new MenuRouterVO();
                //给父级菜单对象赋值
                //框架提供的bean实体类的封装工具类 将指定对象中的相同属性赋值给新对象（目标对象）
                BeanUtils.copyProperties(menu,menuRouterVO);
                //再将没有的属性进行赋值
                MetaVO metaVO = new MetaVO();
                metaVO.setTitle(menu.getName());
                menu.setIcon(menu.getIcon());
                menuRouterVO.setMeta(metaVO);
                //父级菜单的最后一个属性赋值
                //生成children
                Integer menuId = menu.getMenuId();
                List<ChildMenuRouterVO> children = new ArrayList<>();
                //4.不是一级菜单的，继续遍历，找到属于哪个一级菜单下，挂在该菜单下
                //二次遍历，找子菜单
                for (Menu child : menuList) {
                    if(child.getParentId() == menuId){
                        //5.封装子菜单ChildMenuRouterVO，再放进集合List<ChildMenuRouterVO>
                        ChildMenuRouterVO childVO = new ChildMenuRouterVO();
                        BeanUtils.copyProperties(child,childVO);
                        MetaVO childMetaVO = new MetaVO();
                        childMetaVO.setTitle(child.getName());
                        childMetaVO.setIcon(child.getIcon());
                        childVO.setMeta(childMetaVO);
                        children.add(childVO);
                    }
                }
                //6.将子菜单集合挂在MenuRouterVO的children的集合属性下
                menuRouterVO.setChildren(children);
                //7.将每一个MenuRouterVO放进大集合
                list.add(menuRouterVO);
            }

        }

        //8.返回外层大集合List<MenuRouterVO>
        return list;
    }

    /**
     * 分页查询
     * @return
     */
    @Override
    public List<MenuRoleVO> getMenuRoleList() {
        List<Menu> menus = this.menuMapper.selectList(null);
        List<MenuRoleVO> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParentId() == 0){
                MenuRoleVO menuRoleVO = new MenuRoleVO();
                BeanUtils.copyProperties(menu,menuRoleVO);
                menuRoleVO.setId(menu.getMenuId());
                List<MenuRoleVO> children = new ArrayList<>();
                //获取子菜单
                for (Menu item : menus) {
                    if(item.getParentId().equals(menuRoleVO.getId())){
                        MenuRoleVO child = new MenuRoleVO();
                        BeanUtils.copyProperties(item,child);
                        child.setId(item.getMenuId());
                        children.add(child);
                    }
                }
                menuRoleVO.setChildren(children);
                list.add(menuRoleVO);
            }
        }
        return list;
    }
}
